﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

#endregion

namespace ScrumTable.UI.View.Util
{

    /// <summary>
    /// Font Util class
    /// </summary>
    public class FontUtil
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the font size for a control
        /// </summary>
        /// <param name="height">Control height</param>
        /// <param name="width">Control width</param>
        /// <param name="text">Control text</param>
        /// <returns></returns>
        public static double GetFontSize(double height, double width, string text)
        {

            const double sampleFontSize = 1;

            double htRatio;
            double wdRatio;
            double ratio;

            Size textSize = GetSampleSize(text, sampleFontSize);
            double sampleHeight = textSize.Height;
            double sampleWidth = textSize.Width;
            htRatio = height / sampleHeight * 1;
            wdRatio = width / sampleWidth * 1;
            ratio = (htRatio < wdRatio) ? htRatio : wdRatio;
            return (sampleFontSize * ratio);

        }

        private static Size GetSampleSize(string p, double fontSize)
        {

            double sampleFontSize = fontSize;
            String txt = p;
            Typeface myTypeface = new Typeface("Segoe UI");
            FormattedText ft = new FormattedText(txt,
                                                 CultureInfo.CurrentCulture,
                                                 FlowDirection.LeftToRight,
                                                 myTypeface, sampleFontSize, Brushes.Black);
            return new Size(ft.Width, ft.Height);
        }
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion

    }
}
